package com.kkb.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.RoleMapper;
import com.kkb.mapper.UserMapper;
import com.kkb.mapper.UserRoleMapper;
import com.kkb.pojo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserService {
    @Resource
    UserMapper userMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private RoleMapper roleMapper;

    public User queryByUsername(String username) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        criteria.andUsernameEqualTo(username);
        List<User> result = userMapper.selectByExample(userExample);

        return result.size() > 0 ? result.get(0) : null;
    }

    public User queryByUserAndPass(String username,String password) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        criteria.andUsernameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<User> result = userMapper.selectByExample(userExample);

        return result.size() > 0 ? result.get(0) : null;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public PageInfo<User> selectUserInPage(Integer index, Integer size, String username){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username!=null || !"".equals(username.trim())){
            criteria.andUsernameLike("%"+username+"%");
        }
        criteria.andStateEqualTo(0);
        PageHelper.startPage(index,size);
        List<User> users = userMapper.selectByExample(userExample);
        for (User user: users) {
            UserRoleKey userRoleKey = userRoleMapper.selectUserRole(user.getId());
            Integer roleId = userRoleKey.getRoleId();
            Role role = roleMapper.selectByPrimaryKey(roleId);
            user.setRole(role);
        }
        return new PageInfo<User>(users);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteUserById(Integer id){
        User user = new User();
        user.setId(id);
        user.setState(1);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteUserList(List<Integer> list){
        int i =0;
        for (Integer id: list) {
            User user = new User();
            user.setId(id);
            user.setState(1);
            userMapper.updateByPrimaryKeySelective(user);
            i++;
        }
        return i;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int insertUser(User user, Integer roleId){
        int i = userMapper.insertSelective(user);
        UserRoleKey userRoleKey = new UserRoleKey();
        userRoleKey.setUserId(user.getId());
        userRoleKey.setRoleId(roleId);
        int i1 = userRoleMapper.insertSelective(userRoleKey);
        return i+i1;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public User selectUserById(Integer id){
        User user = userMapper.selectByPrimaryKey(id);
        UserRoleKey userRoleKey = userRoleMapper.selectUserRole(user.getId());
        Role role = roleMapper.selectByPrimaryKey(userRoleKey.getRoleId());
        user.setRole(role);
        return user;
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateUser(User user,Integer id){
        int i1 = userMapper.updateByPrimaryKeySelective(user);
        if (id == null)
            id=-1;
        UserRoleKey userRoleKey1 = userRoleMapper.selectUserRole(user.getId());

        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
        criteria.andRoleIdEqualTo(id);
        int i = userRoleMapper.updateByExample(userRoleKey1, userRoleExample);
        return i+i1;
    }
}
